package com.thfw.test.model.bean;

/**
 * Created BY Cuishikun
 * Created ON 2022/2/17
 * Package Name com.thfw.test.model.bean
 **/

import android.content.Context;
import android.net.wifi.ScanResult;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.thfw.test.view.adapter.BlueToothAdapter;
import com.thfw.wmd.R;
import com.thfw.test.view.model.BluetoothLeDevice;

import java.util.ArrayList;
import java.util.List;

/**
 * Created BY Cuishikun
 * Created ON 2021/9/13
 * Package Name com.thfw.test.view.adapter
 **/
//自定义的适配器
public class LightAdapter extends BaseAdapter {
    private List<LampListBean> mlist;
    private List<LampListBean> checkBoxlist;
    private Context mContext;
    private boolean isShowCheckBox;
    private int groupId;
    private boolean onlyShowGroup;
    private boolean isShowGroupLightSmart;
    private boolean isShowAllLightSmart;
    private boolean isButtonSelected;
    private boolean isShowCheckBoxLight;
    private boolean isShowCheckBoxLightSmart;
    OnItemCallBack itemCallBack;

    public LightAdapter(Context context, List<LampListBean> list) {
        this.mContext = context;
        mlist = new ArrayList<LampListBean>();
        this.mlist = list;
    }

    public void setOnClickMyTextView(OnItemCallBack itemCallBack) {
        this.itemCallBack = itemCallBack;
    }

    @Override
    public int getCount() {
        return mlist.size();
    }

    @Override
    public Object getItem(int position) {
        return mlist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void setListAll(List<LampListBean> list) {
        if (mlist == null) {
            mlist = new ArrayList<>();
        }
        mlist.clear();
        mlist.addAll(list);
        notifyDataSetChanged();
    }

    public void clear() {
        if (mlist != null) mlist.clear();
        notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Light person = null;
        if (convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(mContext);
            convertView = inflater.inflate(R.layout.light_item, null);
            person = new Light();
            person.tv_light = (TextView) convertView.findViewById(R.id.tv_light);
            person.cb_lighr = (CheckBox) convertView.findViewById(R.id.cb_lighr);
            person.iv_light = (ImageView) convertView.findViewById(R.id.iv_light);
            convertView.setTag(person);
        } else {
            person = (Light) convertView.getTag();
        }
        //赋值操作
//        person.name.setText(mlist.get(position).toString());
        person.tv_light.setText(mlist.get(position).getLampSn());
        Light finalPerson = person;
        person.iv_light.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!isButtonSelected){
                    if(finalPerson.cb_lighr.isChecked()){
                        finalPerson.cb_lighr.setChecked(false);
                        itemCallBack.onSelectedCallBack(position,true);
                    }else{
                        finalPerson.cb_lighr.setChecked(true);
                        itemCallBack.onSelectedCallBack(position,false);
                    }
                }
            }
        });

        if (mlist.get(position).getIsOpen() != null && mlist.get(position).getIsOpen().equals("1")) {
            person.iv_light.setSelected(true);
        } else {
            person.iv_light.setSelected(false);
        }
        if (!onlyShowGroup) {
            if (isShowCheckBox) {
                person.cb_lighr.setChecked(true);
                person.cb_lighr.setClickable(false);
            } else {
                if (groupId != -2) {
//                    person.cb_lighr.setChecked(false);
                    person.cb_lighr.setClickable(true);
                }else{
                    person.cb_lighr.setClickable(true);
                }
            }
        }

        if (onlyShowGroup) {
            if (mlist.get(position).getGroupId() == groupId) {
                person.cb_lighr.setChecked(true);
            } else {
                person.cb_lighr.setChecked(false);
                person.cb_lighr.setClickable(false);
            }
        }


        return convertView;
    }

    public void setCheckGroupData(int groupId) {
        //反选所有灯

        setAllCheckGroupData(false);
        isButtonSelected = true;
        onlyShowGroup = true;
        //groupId符合的展示
        this.groupId = groupId;
        notifyDataSetChanged();
    }

    public void setAllCheckGroupData(boolean isShowCheckBox) {
        isButtonSelected = false;
        onlyShowGroup = false;
        this.isShowCheckBox = isShowCheckBox;
        this.isButtonSelected = isShowCheckBox;

        notifyDataSetChanged();
    }


    public void setGroupLightSmart(int groupId, boolean isShow, boolean isAll, ArrayList<LampListBean> checkBoxlist) {
        this.groupId = groupId;
        this.checkBoxlist = checkBoxlist;
        ArrayList<LampListBean> lampList = new ArrayList<>();
        for (LampListBean bean : mlist) {
            if (groupId == -1) {
                isShowCheckBoxLight = false;
                if (isShow) {
                    bean.setIsOpen("1");
                } else {
                    bean.setIsOpen("0");
                }
            }

            if (groupId == -2) {
                if (checkBoxlist.size() != 0) {
                    for (LampListBean bean1 : checkBoxlist) {
                        if (bean1.getLampSn().equals(bean.getLampSn())) {
                            if (isShow) {
                                bean.setIsOpen("1");
                            } else {
                                bean.setIsOpen("0");
                            }
                        }
                    }
                }
            }

            if (bean.getGroupId() == groupId && groupId != -1 && groupId != -2) {
                isShowCheckBoxLight = false;
                if (isShow) {
                    bean.setIsOpen("1");
                } else {
                    bean.setIsOpen("0");
                }
            }
            lampList.add(bean);
        }
        setListAll(lampList);
        notifyDataSetChanged();
    }

    public void setCheckBoxSmart(boolean isOpen, boolean isShow) {
        isShowCheckBoxLight = isOpen;
        isShowCheckBoxLightSmart = isShow;

        notifyDataSetChanged();
    }

    public boolean getLightState() {
        boolean b = false;
        for (LampListBean bean : mlist) {
            if (bean.isOpen!=null &&bean.isOpen.equals("1")) {
                b = true;
            }
        }
        return b;
    }

    class Light {
        ImageView iv_light;
        CheckBox cb_lighr;
        TextView tv_light;
    }
}